<template>
  <div class="comment-post">
    <van-field class="post-field" v-model.trim="message" rows="2" autosize
      type="textarea" maxlength="50" placeholder="请输入留言" show-word-limit />
    <van-button class='post-btn' @click="addComment"
      :disabled="!message.length">发布</van-button>
  </div>
</template>

<script>
// 导入添加评论的Api
import { addCommentsApi } from '@/api'
export default {
  name: 'CommentPost',
  components: {},
  props: {
    target: {
      type: [Number, String, Object],
      required: true
    }
  },
  inject: {
    articleId: {
      type: [Number, String, Object],
      default: null
    }
  },
  data () {
    return {
      message: ''
    }
  },
  computed: {},
  watch: {},
  created () { },
  mounted () { },
  methods: {
    async addComment () {
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0
      })
      try {
        const { data } = await addCommentsApi({
          target: this.target.toString(), // 评论的目标id（评论文章即为文章id，对评论进行回复则为评论id）
          content: this.message, // 评论内容
          art_id: this.articleId ? this.articleId.toString() : this.articleId // 文章id，对评论内容发表回复时，需要传递此参数，表明所属文章id。对文章进行评论，不要传此参数。
        })
        console.log(data)
        this.message = ''
        this.$emit('closePopup', data.data)
        this.$toast.success('发表成功')
      } catch (err) {
        console.log('发表评论失败', err)
      }
    }
  }
}
</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  align-items: center;
  padding: 32px 0 32px 32px;
  .post-field {
    background-color: #f5f7f9;
  }
  .post-btn {
    width: 150px;
    border: none;
    padding: 0;
    color: #6ba3d8;
    background: #fff;
    &::before {
      display: none;
    }
  }
}
</style>
